package com.aptemo.webetek.client.widgets.teams;

import com.aptemo.webetek.client.WebetekUserServiceAsync;
import com.aptemo.webetek.client.commands.GotAsynchCallback;
import com.aptemo.webetek.client.commands.impl.GetTeamDetailsAction;
import com.aptemo.webetek.client.enums.EnumUserRole;
import com.aptemo.webetek.client.events.HasDecisionHandlers;
import com.aptemo.webetek.client.exceptions.BackendException.EErrorCodes;
import com.aptemo.webetek.client.mvp.BaseDisplay;
import com.aptemo.webetek.client.mvp.BasePresenter;
import com.aptemo.webetek.shared.TeamBean;
import com.aptemo.webetek.shared.UserBean;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.HasWidgets;

public class TeamHeaderPresenter implements BasePresenter
{
	interface Display extends BaseDisplay
	{

		HasText getTitleField();

		HasText getInfoField();

		HasText getDescField();

		HasValue<Double> getPlayerBalanceField();

		//HasValue<Double> getReinvestField();

		HasClickHandlers getMembersLink();

		HasClickHandlers getPaymentsLink();

		HasDecisionHandlers getReinvestConfirmDialog();

		HasText getRoleField();

		void setMode(EnumUserRole role);

		String translate(String key);

		void setRules(String rules);

		HasText getOwnerField();

		void setHyperlinkTargets(String teamId, String ownerEmail);
	}

	private Display					display;
	private WebetekUserServiceAsync	rpcService;
	private String					teamId;
	protected double				reinvestSum;
	private UserBean				currentUser;

	public TeamHeaderPresenter(String teamId, Display view, HandlerManager eventBus, WebetekUserServiceAsync rpcService, UserBean currentUser)
	{
		//
		this.teamId = teamId;
		this.display = view;
		this.rpcService = rpcService;
		this.currentUser = currentUser;
	}

	private void loadData()
	{
		display.getInfoField().setText(display.translate("loading"));

		GetTeamDetailsAction action = new GetTeamDetailsAction(teamId);
		rpcService.invoke(action, new GotAsynchCallback<TeamBean>()
		{
			@Override
			public void onError(String message, EErrorCodes code)
			{
				display.getInfoField().setText(message);
			}

			@Override
			public void onSuccess(TeamBean result)
			{
				display.getInfoField().setText("");

				display.getTitleField().setText(result.getName());

				display.getDescField().setText(result.getDescription());

				display.setRules(result.getTeamRules());

				display.getOwnerField().setText(result.getOwnerEmail());

				display.setHyperlinkTargets(teamId, result.getOwnerEmail());

				// load user details
				// loadPlayerDetails();
				if (currentUser != null)
				{
					display.setMode(currentUser.getRole());
					display.getRoleField().setText(display.translate(currentUser.getRole().toString()));
					display.getPlayerBalanceField().setValue(currentUser.getBalance());
					//display.getReinvestField().setValue(currentUser.getEarnings());
					if ((currentUser.getRole() == EnumUserRole.EDITOR) || (currentUser.getRole() == EnumUserRole.ADMIN))
					{
						// fill warnings...
						checkWarnings();
					}
				}
				else
				{
					display.setMode(EnumUserRole.GUEST);					
				}

			}

		});

	}

	// private void loadPlayerDetails()
	// {
	// //
	// GetPlayerDetailsAction action = new GetPlayerDetailsAction("", teamId);
	// rpcService.invoke(action, new GotAsynchCallback<UserBean>()
	// {
	//
	// @Override
	// public void onError(String message, EErrorCodes code)
	// {
	// // display.getInfoField().setText(message);
	// display.setMode(EnumRole.GUEST);
	// display.getRoleField().setText(display.translate(EnumRole.GUEST.toString()));
	// }
	//
	// @Override
	// public void onSuccess(UserBean result)
	// {
	// display.setMode(result.getRole());
	//
	// display.getRoleField().setText(display.translate(result.getRole().toString()));
	//
	// display.getPlayerBalanceField().setValue(result.getBalance());
	//
	// // sum earnings...
	// // reinvestSum = reinvestAmount(result.getStakes());
	// // display.getReinvestField().setValue(reinvestSum);
	// display.getReinvestField().setValue(result.getEarnings());
	//
	// if ((result.getRole() == EnumRole.EDITOR) || (result.getRole() ==
	// EnumRole.ADMIN))
	// {
	// // fill warnings...
	// checkWarnings();
	// }
	// }
	// });
	//
	// }

	protected void checkWarnings()
	{
		// check for new join requests...
		int joinCnt = 0;
		int invCnt = 0;
		// TODO: retrieve players...

		// for (UserBean player : team.getPlayers())
		// {
		// if (player.getStatus() == EnumUserStatus.JOINING)
		// {
		// joinCnt++;
		// }
		// else if (player.getStatus() == EnumUserStatus.INVITED)
		// {
		// invCnt++;
		// }
		//
		// }

		if (invCnt > 0)
		{
			String msg = "You have " + invCnt + " invites pending...";
			display.getInfoField().setText(msg);
		}

		if (joinCnt > 0)
		{
			String msg = "You have " + joinCnt + " new join requests...";
			display.getInfoField().setText(msg);
		}

	}

	@Override
	public void go(HasWidgets container)
	{
		container.clear();
		container.add(display.getDisplay());

		bind();

		loadData();
	}

	private void bind()
	{

//		display.getReinvestConfirmDialog().addDecisionHandler(new DecisionHandler()
//		{
//
//			@Override
//			public void onConfirmation(DecisionEvent event)
//			{
//				PaymentAction action = new PaymentAction(reinvestSum);
//				action.setTeamId(teamId);
//				action.setType(EPaymentType.EARNINGS);
//				rpcService.invoke(action, new GotAsynchCallback<UserBean>()
//				{
//
//					@Override
//					public void onError(String message, EErrorCodes code)
//					{
//						display.getInfoField().setText(message);
//					}
//
//					@Override
//					public void onSuccess(UserBean result)
//					{
//						display.getInfoField().setText("Transaction succeeded!");
//					}
//				});
//			}
//		});
	}

}
